package org.snail.employee.service.impl;

import com.dingtalk.open.client.api.model.corp.CorpUserDetail;
import org.snail.common.dingding.utils.JedisUtils;
import org.snail.constant.CacheNameConstant;
import org.snail.employee.bean.EmployeesDO;
import org.snail.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeRedisService {

    @Autowired
    private JedisUtils jedisUtils;

    /**
     * 修改Redis员工数据
     *
     * @param employeesDO
     */
    public int saveOrUpdateEmpToRedis(EmployeesDO employeesDO) throws BaseException {
        int result;
        String hashKey = employeesDO.getEmpId() + "";
        result = saveOrUpdateEmpToRedis(hashKey, employeesDO);
        return result;
    }

    /**
     * 修改Redis员工数据
     *
     * @param hashKey
     * @param employeesDO
     */
    public int saveOrUpdateEmpToRedis(String hashKey, EmployeesDO employeesDO) throws BaseException {
        int result;
        result = jedisUtils.saveHashCache(CacheNameConstant.EMPID_EMPDO_HASH, hashKey, employeesDO);
        return result;
    }


    /**
     * 修改Redis钉钉员工数据
     *
     * @param hashKey
     * @param corpUserDetail
     */
    public int saveOrUpdateDingDingEmpToRedis(String hashKey, CorpUserDetail corpUserDetail) throws BaseException {
        int result;
        result = jedisUtils.saveHashCache(CacheNameConstant.WORKNUM_EMPDO_HASH, hashKey, corpUserDetail);
        return result;
    }

    /**
     * 根据员工id获取员工数据
     *
     * @param employeeId
     */
    public EmployeesDO getEmpFromRedis(String employeeId) throws BaseException {
        EmployeesDO employeesDO = null;
        if (employeeId != null) {
            employeesDO = (EmployeesDO) jedisUtils.getHashCache(CacheNameConstant.EMPID_EMPDO_HASH, employeeId);
        }
        return employeesDO;
    }
}
